/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pagoda.engine.actions;

import java.io.File;
import java.util.Map;
import org.w3c.dom.Node;
import pagoda.engine.TestAction;
import pagoda.engine.TestActionContainer;
import pagoda.engine.TestActionResult;
import pagoda.engine.TestActionStatus;
import pagoda.engine.TestEngine;
import pagoda.util.Shell;

/**
 *
 * @author Kevin Krohe
 */
public class PagodaEngine implements TestEngine {

    private PagodaParser _parser
            = new PagodaParser();

    private TestActionContainer _root
            = new SequenceAction();
    
    public TestAction load(String xml) {
        return _parser.parse(xml, this, _root);
    }

    public TestAction load(File file) {
        return _parser.parse(file, this, _root);
    }

    public TestAction load(Node node) {
        return _parser.parseChildren(node, _root);
    }

    public TestEngine setGlobals(Map<String, Object> globals) {
        _root.getBindings().putAll(globals);
        return this;
    }

    public TestActionStatus execute(String xml) {
        TestAction action = load(xml);
        return action.execute().Status;
    }

    public TestActionStatus execute(File file) {
        TestAction action = load(file);
        return action.execute().Status;
    }

    public TestActionStatus execute(Node node) {
        TestAction action = load(node);
        return action.execute().Status;
    }

    public void echo(String msg) {
        Shell.writeLine(msg);
    }

    public void echo(String tmp, Object... args) {
        Shell.writeLine(tmp, args);
    }

    public void processResult(TestActionResult result) {
        echo(
            "Result Status for Action '%s', was %s \n Message:\t%s",
            result.ActionId,
            result.Status,
            result.Message
            );
    }

}
